{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 字符串基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"python\"\n",
    "s1 = 'python'\n",
    "s2 = \"\"\"python\"\"\"\n",
    "s3 = '''python'''\n",
    "\n",
    "s == s1 == s2 == s3\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 转义字符\n",
    "\n",
    "- \\newline： 接下一行\n",
    "- \\\\ : 表示 \\\n",
    "- \\' : 表示单引号'\n",
    "- \\\" : 表示双引号 \"\n",
    "- \\n ：换行\n",
    "- \\t ：横向制表符\n",
    "- \\b : 退格\n",
    "- \\v : 纵向制表符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\tc\n"
     ]
    }
   ],
   "source": [
    "s = 'a\\nb\\tc'\n",
    "print(s)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 字符串的常用操作\n",
    "\n",
    "- 索引\n",
    "- 切片\n",
    "- 遍历\n",
    "- 字符串不可变(immutable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "pyt\n",
      "p\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "name = \"python\"\n",
    "print(name[0])\n",
    "print(name[0:3])\n",
    "for c in name:\n",
    "    print(c)\n",
    "\n",
    "#name[0] = 'P' # 字符串不可变\n",
    "\n",
    "name = 'P' + name[1:]\n",
    "name = name.replace('h', 'H')\n",
    "\n",
    "\n",
    "name += \"name\"  # name = name + \"name\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python2.5 开始，每次处理字符串的拼接操作时（str1 += str2），Python 首先会检测 str1 还有没有其他的引用。如果没有的话，就会尝试原地扩充字符串 buffer 的大小，而不是重新分配一块内存来创建新的字符串并拷贝。这样的话，上述例子中的时间复杂度就仅为 O(n) 了。\n",
    "\n",
    "- string.strip(str)，表示去掉首尾的 str 字符串；\n",
    "- string.lstrip(str)，表示只去掉开头的 str 字符串；\n",
    "- string.rstrip(str)，表示只去掉尾部的 str 字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 字符串的格式化\n",
    "\n",
    "- string.format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no data available for person with id: 123, name: python\n",
      "no data available for person with id: 123, name: python\n"
     ]
    }
   ],
   "source": [
    "id = 123\n",
    "name = 'python'\n",
    "print('no data available for person with id: {}, name: {}'.format(id, name))\n",
    "\n",
    "print('no data available for person with id: %s, name: %s' % (id, name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = ''\n",
    "for n in range(0, 100000):\n",
    "    s += str(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = []\n",
    "for n in range(0, 100000):\n",
    "    l.append(str(n))\n",
    "    \n",
    "s = ' '.join(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "after json serialization\n",
      "type of params_str = <class 'str'>, params_str = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n",
      "after json deserialization\n",
      "type of original_params = <class 'dict'>, original_params = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "params = {\n",
    "    'symbol': '123456',\n",
    "    'type': 'limit',\n",
    "    'price': 123.4,\n",
    "    'amount': 23\n",
    "}\n",
    "\n",
    "params_str = json.dumps(params)\n",
    "\n",
    "print('after json serialization')\n",
    "print('type of params_str = {}, params_str = {}'.format(type(params_str), params))\n",
    "\n",
    "original_params = json.loads(params_str)\n",
    "\n",
    "print('after json deserialization')\n",
    "print('type of original_params = {}, original_params = {}'.format(type(original_params), original_params))\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
